package com.ll.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ll.blog.common.ApiResponse;
import com.ll.blog.contant.ExceptionEnum;
import com.ll.blog.entity.Article;
import com.ll.blog.entity.Categories;
import com.ll.blog.model.res.CategoriesRes;
import com.ll.blog.service.IArticleService;
import com.ll.blog.service.ICategoriesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 分类模块
 *
 * @author
 * @since 2023-10-25
 */
@RestController
@Validated
public class CategoriesController {
    @Autowired
    ICategoriesService categoriesService;
    @Autowired
    IArticleService articleService;

    /**
     * 添加分类接口
     *
     * @param categoryName 分类名
     */
    @PostMapping("/admin/categories/add")
    public ApiResponse addCategory(@NotEmpty(message = "分类名称不能为空") String categoryName) {
        Categories categories = new Categories();
        categories.setName(categoryName);
        boolean saved = categoriesService.save(categories);
        if (saved) {
            return ApiResponse.success();
        } else {
            return ApiResponse.error(ExceptionEnum.SAVE_ERROR);
        }
    }

    /**
     * 查询所有分类
     */
    @GetMapping("categories/list")
    public ApiResponse categoriesListAll() {
        List<Categories> categoriesList = categoriesService.list();
        return ApiResponse.success(categoriesList);
    }

    @GetMapping("categories/listAndNum")
    public ApiResponse categoriesListAndNum() {
        List<Categories> categoriesList = categoriesService.list();
        ArrayList<CategoriesRes> list = new ArrayList<>();
        for (Categories categories : categoriesList) {
            long count = articleService.count(new LambdaQueryWrapper<Article>().eq(Article::getCategoryId, categories.getId()));
            CategoriesRes res = new CategoriesRes();
            BeanUtils.copyProperties(categories,res);
            res.setNum(count);
            list.add(res);
        }
        return ApiResponse.success(list);
    }

    /**
     * 修改分类
     * @param categoryName 分类名称
     * @param id id
     */
    @PutMapping("/admin/categories/{id}")
    public ApiResponse edit(@NotEmpty(message = "分类名称不能为空") String categoryName
            , @PathVariable Integer id) {
        Categories categories = new Categories();
        categories.setId(id);
        categories.setName(categoryName);
        boolean b = categoriesService.updateById(categories);
        if (!b) {
            return ApiResponse.error(ExceptionEnum.UPDATE_ERROR);
        }
        return ApiResponse.success();
    }

    /**
     * 删除分类接口
     * @param id
     * @return
     */
    @DeleteMapping("/admin/class/{id}")

    public ApiResponse del(@PathVariable Integer id){
        boolean b = categoriesService.removeById(id);
        if (!b) {
            return ApiResponse.error(ExceptionEnum.DELETE_ERROR);
        }
        return ApiResponse.success("删除成功");
    }
}
